package com.rgs.base.core.util.excel;

import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.util.StyleUtil;
import com.alibaba.excel.write.style.AbstractCellStyleStrategy;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Ren Gaoshuai
 * @date 2022-03-08 15:45
 **/
public class HorizontalCellPlusStyleStrategy extends AbstractCellStyleStrategy {
    private WriteCellPlusStyle headWriteCellStyle;
    private List<WriteCellPlusStyle> contentWriteCellStyleList;

    private XSSFCellStyle headCellStyle;
    private List<XSSFCellStyle> contentCellStyleList;

    public HorizontalCellPlusStyleStrategy(WriteCellPlusStyle headWriteCellStyle,
                                           List<WriteCellPlusStyle> contentWriteCellStyleList) {
        this.headWriteCellStyle = headWriteCellStyle;
        this.contentWriteCellStyleList = contentWriteCellStyleList;
    }

    public HorizontalCellPlusStyleStrategy(WriteCellPlusStyle headWriteCellStyle, WriteCellPlusStyle contentWriteCellStyle) {
        this.headWriteCellStyle = headWriteCellStyle;
        contentWriteCellStyleList = new ArrayList<>();
        contentWriteCellStyleList.add(contentWriteCellStyle);
    }

    private XSSFCellStyle buildCellStyle(XSSFCellStyle xssfCellStyle, WriteCellPlusStyle headWriteCellStyle) {
        if (headWriteCellStyle.getFillForegroundXSSFColor() != null) {
            xssfCellStyle.setFillForegroundColor(headWriteCellStyle.
                    getFillForegroundXSSFColor());
        }
        if (headWriteCellStyle.getFillBackgroundXSSFColor() != null) {
            xssfCellStyle.setFillBackgroundColor(headWriteCellStyle.
                    getFillBackgroundXSSFColor());
        }
        return xssfCellStyle;
    }

    @Override
    protected void initCellStyle(Workbook workbook) {
        if (headWriteCellStyle != null) {
            headCellStyle = buildCellStyle((XSSFCellStyle)
                    StyleUtil.buildHeadCellStyle(workbook, headWriteCellStyle), headWriteCellStyle);
        }
        if (contentWriteCellStyleList != null && !contentWriteCellStyleList.isEmpty()) {
            contentCellStyleList = new ArrayList<>();
            for (WriteCellPlusStyle writeCellStyle : contentWriteCellStyleList) {
                contentCellStyleList.add(
                        buildCellStyle(
                                (XSSFCellStyle)
                                        StyleUtil.buildContentCellStyle(workbook, writeCellStyle),
                                writeCellStyle));
            }
        }
    }

    @Override
    protected void setHeadCellStyle(Cell cell, Head head, Integer relativeRowIndex) {
        if (headCellStyle == null) {
            return;
        }
        cell.setCellStyle(headCellStyle);
    }

    @Override
    protected void setContentCellStyle(Cell cell, Head head, Integer relativeRowIndex) {
        if (contentCellStyleList == null || contentCellStyleList.isEmpty()) {
            return;
        }
        cell.setCellStyle(contentCellStyleList.get(relativeRowIndex % contentCellStyleList.size()));
    }


}
